package sg.edu.nus.iss.awsdynamodb;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodb.model.AttributeValue;
import com.amazonaws.services.dynamodb.model.ComparisonOperator;
import com.amazonaws.services.dynamodb.model.Condition;
import com.amazonaws.services.dynamodb.model.DeleteItemRequest;
import com.amazonaws.services.dynamodb.model.DeleteItemResult;
import com.amazonaws.services.dynamodb.model.Key;
import com.amazonaws.services.dynamodb.model.PutItemRequest;
import com.amazonaws.services.dynamodb.model.PutItemResult;
import com.amazonaws.services.dynamodb.model.QueryRequest;
import com.amazonaws.services.dynamodb.model.QueryResult;

import sg.edu.nus.iss.CONFIG;
import sg.edu.nus.iss.Util;
import sg.edu.nus.iss.stockex.RelativeStrengthIndex;

public class RelativeStrengthIndexTable implements DynamoDBTableInf<RelativeStrengthIndex> {
	private AmazonDynamoDBClient dynamoDB = DynamoDB.client();
	@Override
	public Map<String, AttributeValue> newItem(RelativeStrengthIndex item) {
		Map<String, AttributeValue> map = new HashMap<String, AttributeValue>();
		map.put("code", new AttributeValue(item.getCode()));
		map.put("date", new AttributeValue(item.getDate()));
		map.put("rsi", new AttributeValue().withN(Double.toString(item.getRsi())));
		return map;
	}

	@Override
	public boolean Add(RelativeStrengthIndex item) {
		 boolean status = false;
		 try{
		 Map<String,AttributeValue> map = newItem(item);
		 PutItemRequest putItemRequest = new PutItemRequest(CONFIG.RELATIVE_STRENGHT_INDEX_TABLE_NAME, map);
		 PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
		 System.out.println("Result: " + putItemResult);
		 if(putItemResult!=null)
		 {
			 status =  true;
		 }
		 }catch (Exception e) {
			 e.printStackTrace();
			// TODO: handle exception
		}
		
		 return status;
	}

	@Override
	public boolean Delete(RelativeStrengthIndex item) {
		boolean status = false;
		Key key = new Key();
		key.setHashKeyElement(new AttributeValue(item.getCode()));
		key.setRangeKeyElement(new AttributeValue(item.getDate()));
		try{
			DeleteItemRequest deleteItemRequest = new DeleteItemRequest().withTableName(CONFIG.RELATIVE_STRENGHT_INDEX_TABLE_NAME).withKey(key);
			DeleteItemResult result = dynamoDB.deleteItem(deleteItemRequest);
			if(result!=null)
			{
				status = true;	
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
		return status;
	}
	//past 30 days based on code and date..
	public ArrayList<RelativeStrengthIndex> query(String code, String date)
	{
		ArrayList<RelativeStrengthIndex> rsiList = new ArrayList<RelativeStrengthIndex>();
		String backDate = Util.oneMonthBackDate(date);
		System.out.println("### Date = " + date);
		System.out.println("### Backdate = " + backDate);
		Condition rangeKeyCondition = new Condition()
		.withComparisonOperator(ComparisonOperator.BETWEEN.toString())
		.withAttributeValueList(new AttributeValue().withS(backDate), new AttributeValue().withS(date));
		
		QueryRequest queryRequest = new QueryRequest().withTableName(CONFIG.RELATIVE_STRENGHT_INDEX_TABLE_NAME)
				.withHashKeyValue(new AttributeValue().withS(code))
				.withRangeKeyCondition(rangeKeyCondition);
		QueryResult result = dynamoDB.query(queryRequest);
		for (Map<String, AttributeValue> item : result.getItems()) {
			rsiList.add(convert(item));
		}
		return rsiList;
	}
	
	public RelativeStrengthIndex convert(Map<String, AttributeValue> item)
	{
		RelativeStrengthIndex rsi = null;
		String code = item.get("code").getS();
		String date = item.get("date").getS();
		double rsiDobule = new Double(item.get("rsi").getN()).doubleValue();
		rsi = new RelativeStrengthIndex(code, date, rsiDobule);
		return rsi;
	}
}
